package com.ian.controller;

import com.common.util.ResultMsg;
import com.ian.pojo.PerArticle;
import com.ian.pojo.PerAtlike;
import com.ian.pojo.PerStall;
import com.ian.pojo.PerStlike;
import com.ian.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 收藏服务
 *
 * @Author IanZy
 * @Date 2020/6/20
 */
@Api("用户收藏相关API")
@RestController
public class PerLikeController {
    @Autowired
    PerLikeService perLikeService;

    @Autowired
    PerArticleService perArticleService;

    @Autowired
    PerUserService perUserService;

    @Autowired
    PerStallService perStallService;

    @Autowired
    PerAtlikeService perAtlikeService;

    @Autowired
    PerStlikeService perStlikeService;

    /**
     * 收藏文章
     *
     * @Author IanZy
     * @Date 2020/6/22
     */
    @ApiOperation("收藏文章")
    @PostMapping(value = "/collectArticle", produces = "application/json;charset=UTF-8")
    public ResultMsg collectArticle(@RequestParam("mobile") String mobile, @RequestParam("id") int id) {
        if (perArticleService.selectByPrimaryKey(id).getPerArticleFavorite() == 0) {
//            PerLike perLike = new PerLike();
//            perLike.setPerUserId(perUserService.findByTel(mobile).getPerUserId());
//            perLike.setPerArticleId(id);
//            perLikeService.insert(perLike);
            PerAtlike perAtlike = new PerAtlike();
            perAtlike.setPerUserId(perUserService.findByTel(mobile).getPerUserId());
            perAtlike.setPerArticleId(id);
            perAtlikeService.insert(perAtlike);
            PerArticle perArticle = new PerArticle();
            perArticle.setPerArticleId(id);
            perArticle.setPerArticleTitle(perArticleService.selectByPrimaryKey(id).getPerArticleTitle());
            perArticle.setPerArticleDetails(perArticleService.selectByPrimaryKey(id).getPerArticleDetails());
            perArticle.setPerArticleImg(perArticleService.selectByPrimaryKey(id).getPerArticleImg());
            perArticle.setPerArticleFavorite(1);
            perArticle.setPerArticleSubtitle(perArticleService.selectByPrimaryKey(id).getPerArticleSubtitle());
            perArticle.setPerArticleTime(perArticleService.selectByPrimaryKey(id).getPerArticleTime());
            perArticleService.updateByPrimaryKey(perArticle);
            return ResultMsg.success("收藏成功~", mobile);
        }
        PerArticle perArticle = new PerArticle();
        perArticle.setPerArticleId(id);
        perArticle.setPerArticleTitle(perArticleService.selectByPrimaryKey(id).getPerArticleTitle());
        perArticle.setPerArticleDetails(perArticleService.selectByPrimaryKey(id).getPerArticleDetails());
        perArticle.setPerArticleImg(perArticleService.selectByPrimaryKey(id).getPerArticleImg());
        perArticle.setPerArticleFavorite(0);
        perArticle.setPerArticleSubtitle(perArticleService.selectByPrimaryKey(id).getPerArticleSubtitle());
        perArticle.setPerArticleTime(perArticleService.selectByPrimaryKey(id).getPerArticleTime());
        perArticleService.updateByPrimaryKey(perArticle);
//        perLikeService.deleteByPrimaryKey(perLikeService.selectByArticleId(id).getPerLikeId());
        perAtlikeService.deleteByPrimaryKey(perAtlikeService.selectByArticleId(id).getPerAtlikeId());
        return ResultMsg.failed("取消收藏成功~");
    }

    /**
     * 查询登录用户文章收藏列表
     *
     * @Author IanZy
     * @Date 2020/6/22
     */
    @ApiOperation("查询用户收藏文章列表")
    @GetMapping(value = "/myFavorite", produces = "application/json;charset=UTF-8")
    public ResultMsg myFavorite(@RequestParam("mobile") String mobile) {
        if (mobile != null) {
            Integer uid = perUserService.findByTel(mobile).getPerUserId();
//            List<PerLike> perLikes = perLikeService.selectByUid(uid);
            List<PerArticle> perArticles = new ArrayList<>();
            List<PerAtlike> perLikes = perAtlikeService.selectByUid(uid);
            for (PerAtlike perAtlike : perLikes) {
                PerArticle perArticle = perArticleService.selectByPrimaryKey(perAtlike.getPerArticleId());
                perArticles.add(perArticle);
            }
            return ResultMsg.success("查询收藏列表成功~", perArticles);
        }
        return ResultMsg.failed("查询收藏列表失败~");
    }

    /**
     * 删除用户收藏文章
     *
     * @param id
     * @return
     */
    @ApiOperation("删除用户收藏文章")
    @DeleteMapping(value = "/deleteFavorite", produces = "application/json;charset=UTF-8")
    public ResultMsg deleteFavorite(@RequestParam("id") int id) {
        if (id != 0) {
//            perLikeService.deleteByArticleId(id);
            perAtlikeService.deleteByArticleId(id);
            PerArticle perArticle = perArticleService.selectByPrimaryKey(id);
            PerArticle article = new PerArticle();
            article.setPerArticleId(id);
            article.setPerArticleTime(perArticle.getPerArticleTime());
            article.setPerArticleSubtitle(perArticle.getPerArticleSubtitle());
            article.setPerArticleFavorite(0);
            article.setPerArticleImg(perArticle.getPerArticleImg());
            article.setPerArticleDetails(perArticle.getPerArticleDetails());
            article.setPerArticleTitle(perArticle.getPerArticleTitle());
            perArticleService.updateByPrimaryKey(article);
            return ResultMsg.success("删除收藏成功~", id);
        }
        return ResultMsg.failed("删除收藏失败~");
    }

    /**
     * 收藏车位
     *
     * @param stallId
     * @return
     */
    @ApiOperation("收藏车位")
    @PostMapping(value = "/collectStall", produces = "application/json;charset=UTF-8")
    public ResultMsg collectStall(@RequestParam("stallId") int stallId) {
        PerStall stall = perStallService.selectByPrimaryKey(stallId);
        if (stall.getPerStallFavorite() == 0) {
//            PerLike perLike = new PerLike();
//            perLike.setPerUserId(perStallService.selectByPrimaryKey(stallId).getPerUserId());
//            perLike.setPerStallId(stallId);
//            perLikeService.insert(perLike);
            PerStlike perStlike = new PerStlike();
            perStlike.setPerUserId(perStallService.selectByPrimaryKey(stallId).getPerUserId());
            perStlike.setPerStallId(stallId);
            perStlikeService.insert(perStlike);
            PerStall perStall = new PerStall();
            perStall.setPerStallId(stallId);
            perStall.setPerStallAddress(stall.getPerStallAddress());
            perStall.setPerStallLnglat(stall.getPerStallLnglat());
            perStall.setPerStallStatus(stall.getPerStallStatus());
            perStall.setPerStallStarttime(stall.getPerStallStarttime());
            perStall.setPerStallEndtime(stall.getPerStallEndtime());
            perStall.setPerStallPrice(stall.getPerStallPrice());
            perStall.setPerStallAmount(stall.getPerStallAmount());
            perStall.setPerStallNum(stall.getPerStallNum());
            perStall.setPerStallComment(stall.getPerStallComment());
            perStall.setPerUserId(stall.getPerUserId());
            perStall.setPerStallFavorite(1);
            perStallService.updateByPrimaryKey(perStall);
            return ResultMsg.success("收藏成功~", stallId);
        }
        PerStall perStall = new PerStall();
        perStall.setPerStallId(stallId);
        perStall.setPerStallAddress(stall.getPerStallAddress());
        perStall.setPerStallLnglat(stall.getPerStallLnglat());
        perStall.setPerStallStatus(stall.getPerStallStatus());
        perStall.setPerStallStarttime(stall.getPerStallStarttime());
        perStall.setPerStallEndtime(stall.getPerStallEndtime());
        perStall.setPerStallPrice(stall.getPerStallPrice());
        perStall.setPerStallAmount(stall.getPerStallAmount());
        perStall.setPerStallNum(stall.getPerStallNum());
        perStall.setPerStallComment(stall.getPerStallComment());
        perStall.setPerUserId(stall.getPerUserId());
        perStall.setPerStallFavorite(0);
        perStallService.updateByPrimaryKey(perStall);
//        perLikeService.deleteByPrimaryKey(perLikeService.selectByStallId(stallId).getPerLikeId());
        perStlikeService.deleteByPrimaryKey(perStlikeService.selectByStallId(stallId).getPerStlikeId());
        return ResultMsg.failed("取消收藏成功");
    }

    /**
     * 查询用户收藏车位列表
     *
     * @param mobile
     * @return
     */
    @ApiOperation("查询用户收藏车位列表")
    @GetMapping(value = "/myFavoriteStall", produces = "application/json;charset=UTF-8")
    public ResultMsg myFavoriteStall(@RequestParam("mobile") String mobile) {
        if (mobile != null) {
            Integer uid = perUserService.findByTel(mobile).getPerUserId();
//            List<PerLike> perLikes = perLikeService.selectByUid(uid);
            List<PerStlike> perLikes = perStlikeService.selectByUid(uid);
            List<PerStall> perStalls = new ArrayList<>();
            for (PerStlike perStlike : perLikes) {
                PerStall perStall = perStallService.selectByPrimaryKey(perStlike.getPerStallId());
                perStalls.add(perStall);
            }
            return ResultMsg.success("查询收藏列表成功~", perStalls);
        }
        return ResultMsg.failed("查询收藏列表失败~");
    }

    /**
     * 删除用户收藏车位
     *
     * @param id
     * @return
     */
    @ApiOperation("删除用户收藏车位")
    @DeleteMapping(value = "/deleteFavoriteStall", produces = "application/json;charset=UTF-8")
    public ResultMsg deleteFavoriteStall(@RequestParam("id") int id) {
//        perLikeService.deleteByStallId(id);
        perStlikeService.selectByStallId(id);
        PerStall perStall = perStallService.selectByPrimaryKey(id);
        PerStall stall = new PerStall();
        stall.setPerUserId(perStall.getPerUserId());
        stall.setPerStallComment(perStall.getPerStallComment());
        stall.setPerStallNum(perStall.getPerStallNum());
        stall.setPerStallAmount(perStall.getPerStallAmount());
        stall.setPerStallPrice(perStall.getPerStallPrice());
        stall.setPerStallEndtime(perStall.getPerStallEndtime());
        stall.setPerStallStarttime(perStall.getPerStallStarttime());
        stall.setPerStallStatus(perStall.getPerStallStatus());
        stall.setPerStallLnglat(perStall.getPerStallLnglat());
        stall.setPerStallAddress(perStall.getPerStallAddress());
        stall.setPerStallId(id);
        stall.setPerStallFavorite(0);
        perStallService.updateByPrimaryKey(stall);
        return ResultMsg.success("删除收藏成功~", id);
    }
}